import qs from "qs"
import ManageConstant from "../consts/manageConstant";
import cookies from "js-cookie";

class CollectionService {

  constructor(_this) {
    this._this = _this;
    this._axios = _this.axios.create({
      baseURL: ManageConstant.DOMAIN_URL,
      timeout: 5000,
      headers: {
        Token: cookies.get('Token')
      }
    });
  }

  searchCollection(params, executeMethod){
    this._axios.get(ManageConstant.COLLECTION_SEARCH, {params}).then(res => {
      if (res.data.resultCode == 0) {
        executeMethod(this._this, res.data);
      }
    })
  }

  addCollection(params){
    this._axios.post(ManageConstant.COLLECTION_ADD, params).then(res => {
      if (res.data.resultCode == 0) {
        this._this.$Message.success("收藏成功");
      }
      else if(res.data.resultCode == 1018){
        this._this.$Message.error("收藏夹已收藏过该视频");
      }
      else{
        this._this.$Message.error("收藏失败");
      }
    })
  }

  batchDeleteCollections(collectionIds, executeMethod){
    this._axios.post(ManageConstant.COLLECTION_BATCH_DELETE, qs.stringify({
      collectionIds
    }, {
      indices: false
    })).then(res => {
      if (res.data.resultCode == 0) {
        this._this.$Message.success("取消收藏成功");
        executeMethod(this._this, res.data);
      }
      else{
        this._this.$Message.error("取消收藏失败");
      }
    })
  }

}
export default CollectionService;
